package com.web.controller;

import com.web.bean.PageModel;
import com.web.bean.选课及成绩;
import com.web.dao.选课及成绩Dao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//定义当前请求处理的访问uri
@WebServlet("/getAllAchievement")
public class AchievementQueryController extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String pageNo = req.getParameter("pageNo");
        String search = req.getParameter("search");
        if(search == null){
            search = "";
        }
        int pageNum = 1;
        if (pageNo != null && !pageNo.equals("")) {
            pageNum = Integer.parseInt(pageNo);
        }

        选课及成绩Dao dao = new 选课及成绩Dao();
        PageModel page = new PageModel(pageNum, dao.queryAchievementCount(search));
        List<选课及成绩> list = null;
        try {
            list = dao.queryAchievement(search,page);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("list", list);
        req.setAttribute("pageInfo", page);
        req.setAttribute("search", search);
        req.getRequestDispatcher("/AchievementIndex.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}